package ink.onei.framework.web.service;

import ink.onei.common.constant.CacheConstants;
import ink.onei.common.constant.Constants;
import ink.onei.common.core.domain.entity.Seller;
import ink.onei.common.core.domain.entity.SysUser;
import ink.onei.common.core.domain.model.RegisterBody;
import ink.onei.common.core.redis.RedisCache;
import ink.onei.common.exception.user.CaptchaException;
import ink.onei.common.exception.user.CaptchaExpireException;
import ink.onei.common.utils.MessageUtils;
import ink.onei.common.utils.SecurityUtils;
import ink.onei.common.utils.StringUtils;
import ink.onei.framework.manager.AsyncManager;
import ink.onei.framework.manager.factory.AsyncFactory;
import ink.onei.system.service.ISysConfigService;
import ink.onei.system.service.ISysUserService;
import ink.onei.user.service.ISellerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 注册校验方法
 *
 * @author nekotako
 */
@Component
public class SysRegisterService {
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISellerService sellerService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 系统用户注册
     */
    public String systemRegister(RegisterBody registerBody) {
        String msg = "", username = registerBody.getUsername(), password = registerBody.getPassword();
        SysUser sysUser = new SysUser();
        sysUser.setUserName(username);

        // 验证码开关
        boolean captchaEnabled = configService.selectCaptchaEnabled();
        if (captchaEnabled) {
            validateCaptcha(username, registerBody.getCode(), registerBody.getUuid());
        }

        if (!userService.checkUserNameUnique(sysUser)) {
            msg = "账号已存在";
        } else {
            sysUser.setNickName(username);
            sysUser.setPassword(SecurityUtils.encryptPassword(password));
            boolean regFlag = userService.registerUser(sysUser);
            if (!regFlag) msg = "注册失败,请联系系统管理人员"; else AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.REGISTER, MessageUtils.message("user.register.success")));
        }
        return msg;
    }

    /**
     * 卖家用户注册
     */
    public String sellerRegister(RegisterBody registerBody) {
        String msg = "", username = registerBody.getUsername(), password = registerBody.getPassword();
        Seller seller = new Seller();
        seller.setUsername(username);

        // 验证码开关
        boolean captchaEnabled = configService.selectCaptchaEnabled();
        if (captchaEnabled) {
            validateCaptcha(username, registerBody.getCode(), registerBody.getUuid());
        }

        if (sellerService.getSellerDetail(seller) != null) msg = "账号已存在";
        else {
            Integer regFlag = sellerService.register(username, password);
            if (regFlag == 0) msg = "注册失败,请联系系统管理人员"; else AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.REGISTER, MessageUtils.message("user.register.success")));

        }
        return msg;
    }

    /**
     * 校验验证码
     *
     * @param username 用户名
     * @param code     验证码
     * @param uuid     唯一标识
     * @return 结果
     */
    public void validateCaptcha(String username, String code, String uuid) {
        String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + StringUtils.nvl(uuid, "");
        String captcha = redisCache.getCacheObject(verifyKey);
        redisCache.deleteObject(verifyKey);
        if (captcha == null) {
            throw new CaptchaExpireException();
        }
        if (!code.equalsIgnoreCase(captcha)) {
            throw new CaptchaException();
        }
    }
}
